#pragma once

#include<iostream>
#include<string>
#include<vector>
#include<unordered_map>
#include<fstream>
#include"util.hpp"

namespace ns_index
{
    struct DocInfo
    {
        std::string title;
        std::string content;
        std::string url;
        uint64_t doc_id;
    };
    struct InvertedElem
    {
        uint64_t doc_id;
        std::string word;
        int weight;

    };
    
    typedef std::vector<InvertedElem> InvertedList;

    class Index
    {
        private:
        //正排索引采用顺序表结构，建立ID和文档间的对应关系
        std::vector<DocInfo> forward_index; //正排索引
        //倒排索引采用哈希结构，建立词和多个文档间的关系
        std::unordered_map<std::string,InvertedList> inverted_index;//倒排索引
        private:
        Index(){}
        Index(const Index&) = delete;
        Index& operator=(const Index&) = delete;
        static Index* instance;
        static std::mutex mtx;
        public:
        ~Index(){}

        static Index* GetInstance()
        {
            if(instance==nullptr)
            {
                mtx.lock();
                if(instance==nullptr)
                {
                    instance = new Index();
                }
                mtx.unlock();
            }
            return instance;
        }

        DocInfo* GetForwardList(const uint64_t doc_id)
        {
            if(doc_id>=forward_index.size())
            {
                std::cerr<<"doc_id out range,error!"<<std::endl;
                return nullptr;
            }
            return &forward_index[doc_id];
        }
        InvertedList* GetInvertedLise(const std::string &word)
        {
            auto iter = inverted_index.find(word);
            if(iter==inverted_index.end())
            {
                std::cerr << word << " have no InvertedList" << std::endl;
                return nullptr;
            }
            return &(iter->second);
        }
        bool BuildIndex(const std::string &input)
        {
            std::ifstream in(input, std::ios::in | std::ios::binary);
                if(!in.is_open()){
                    std::cerr << "sorry, " << input << " open error" << std::endl;
                    return false;
                }
            std::string line;
            int count = 0;
            while(std::getline(in,line))
            {
                DocInfo* doc = BuildForwardIndex(line);
                if(doc==nullptr)
                {
                    std::cerr << "build " << line << " error" << std::endl; //for deubg
                    continue;
                }
                BulidInvertedIndex(*doc);
                count++;
                if(count%50==0)
                {
                    // std::cout<<"当前已经建立的文档索引： "<<count<<std::endl;
                    LOG(NORMAL, "当前的已经建立的索引文档: " + std::to_string(count));
                }

            }
            return true;
        }
        private:
        DocInfo* BuildForwardIndex(const std::string &line)
        {
            //解析line，字符串切分
            std::vector<std::string> results;
            const std::string sep = "\3";
            ns_util::StringUtil::Split(line,&results,sep);
            if(results.size()!=3)
            {
                return nullptr;
            }
            //将切分后的字符串填充到DocInfo
            DocInfo doc;
            doc.title = results[0];
            doc.content = results[1];
            doc.url = results[2];
            doc.doc_id = forward_index.size();
            //插入到正排索引的vector中
            forward_index.push_back(std::move(doc));
            //返回当前插入的docinfo，方便后续建立倒排索引
            return &forward_index.back();
        }
        bool BulidInvertedIndex(const DocInfo &doc)
        {
            struct word_cnt
            {
                int title_cnt;
                int content_cnt;
                word_cnt():title_cnt(0),content_cnt(0){}
            };
            std::unordered_map<std::string,word_cnt> word_map;
            std::vector<std::string> title_words;
            std::vector<std::string> content_words;

            ns_util::JiebaUtil::CutString(doc.title,&title_words);
            ns_util::JiebaUtil::CutString(doc.content,&content_words);
            for(auto s:title_words)
            {
                boost::to_lower(s);
                word_map[s].title_cnt++;
            }
            for(auto s:content_words)
            {
                boost::to_lower(s);
                word_map[s].content_cnt++;
            }
            #define X 10
            #define Y 1
            for(auto&word_pair:word_map)
            {
                InvertedElem item;
                item.doc_id = doc.doc_id;
                item.word = word_pair.first;
                item.weight = word_pair.second.title_cnt*X+word_pair.second.content_cnt*Y;
                InvertedList &inverted_list = inverted_index[word_pair.first];
                inverted_list.push_back(std::move(item));
            }
            return true;

        }
    };
    Index* Index::instance = nullptr;
    std::mutex Index::mtx;
}